<?php 
$weekarr = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
$week = $weekarr[date("w")];
?>
<div id="order" class="indexcontentdiv">
    <div class="positiondiv">
        <table>
            <tr>
                <td class="td-left">
                    <img src="<?=base_url()?>resource/img/home_ico.png" width="21" height="17">
                    Data Entry > <a href="<?=base_url()?>profile/order">Order</a> > New Order
                </td>
                <td class="td-right">
                    <img src="<?=base_url()?>resource/img/menu2.png" width="27" height="24">Today is <?=$week?>,<?=date('M d,Y')?>
                </td>
            </tr>
        </table>
    </div>
    <div class="input-div">
        <form action="<?=base_url('profile/new_order')?>" method="post" onkeydown="if(event.keyCode==13)return false;" onsubmit="return checkform()">
        <table>
            <tr><td colspan="2"><h3>.Order</h3></td></tr>
            <tr><td class="nametd">Order No:</td><td><input type="text" name="sn"/><span id="snmsg" class="err">*</span></td></tr>
            <tr>
                <td class="nametd">Company Name:</td>
                <td>
                    <input type="text" name="name" id="name" readonly="readonly" title=""/>
                    <input type="hidden" name="companyid" id="companyid"/>
                </td>
            </tr>
            <tr><td class="nametd">Position:</td><td><input type="text" name="position"/><span id="positionmsg" class="err">*</span></td></tr>
            <tr><td class="nametd">Salary:</td><td><input type="text" name="salary"/><span id="salarymsg" class="err">*</span></td></tr>
            <tr>
                <td class="nametd">Gender:</td>
                <td>
                    <select name="gender" id="gender" style="width:120px">
                        <option value="0">Male or Female
                        <option value="1">Male
                        <option value="2">Female
                    </select>
                </td>
            </tr>
<!--            <tr>
                <td class="nametd">Workers:</td>
                <td>
                    <input type="text" name="workers" id="workers" readonly="readonly" title=""/>
                    <input type="hidden" name="workerids" id="workerids"/>
                </td>
            </tr>-->
            <tr><td class="nametd">Work Intro:</td>
                <td>
                    <textarea name="workintro"></textarea>
                </td>
            </tr>
            <tr>
                <td class="nametd">Priority:</td>
                <td>
                    <select name="priority" id="priority" style="width:120px;">
                        <option value="0">Other
                        <option value="1">A
                        <option value="2">B
                        <option value="3">C
                    </select>
                </td>
            </tr>
            <tr>
                <td class="nametd">Work Type:</td>
                <td>
                    <select name="worktype" id="worktype" style="width:120px;">
                        <option value="1">WP
                        <option value="2">SP
                        <option value="3">EP
                        <option value="0">Other   
                    </select>
                </td>
            </tr>
<!--            <tr>
                <td class="nametd">Work Time:</td>
                <td>
                    <input type="text" name="s_time" id="s_time" placeholder="Start time" style="width:120px;"/>
                    -
                    <input type="text" name="e_time" id="e_time" placeholder="End time" style="width:120px;"/>
                    <span id="timemsg" class="err"></span>
                </td>
            </tr>-->
            <tr><td class="nametd">Salesman:</td>
                <td>
                    <select name="saleser" id="saleser" style="width:140px;">
                        <option value="0">-Saleser-
                        <?php foreach ($marketing as $key => $value) {?>
                            <option value="<?=$value['id']?>"><?=$value['username']?>
                        <?php }?>
                    </select>
                </td>
            </tr>
            <tr><td class="nametd">Comments:</td>
                <td>
                    <textarea name="comment"></textarea>
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="submit" value="Save"/>
                    <input type="button" value="Reset"/>
                </td>
            </tr>
        </table>
        </form>
    </div>
</div>
<script>
function checkEndTime(){  
    var startTime=$("input[name='s_time']").val();  
    var endTime=$("input[name='e_time']").val();
    if(startTime != '' && endTime != ''){
        var start=new Date(startTime.replace("-", "/").replace("-", "/")); 
        var end=new Date(endTime.replace("-", "/").replace("-", "/"));
        if(end<start){  
            return false;  
        }  
        return true;
    }else{
        return true;
    }
      
}

$("input[type='button']").click(function(){
    $("input[name='sn']").val('');
    $("input[name='name']").val('');
    $("input[name='companysn']").val('');
    $("#gender").val(0);
    $("#worktype").val(1);
    $("input[name='position']").val('');
    $("input[name='salary']").val('');
    $("textarea[name='workintro']").val('');
    $("input[name='s_time']").val('');
    $("input[name='e_time']").val('');
    $("textarea[name='comment']").val('');
});

$('#s_time').datetimepicker({
//    lang:"ch",           //语言选择中文
    format:"Y-m-d",      //格式化日期
    timepicker:false,    //关闭时间选项
//    yearStart：2000,     //设置最小年份
//    yearEnd:2050,        //设置最大年份
//    todayButton:false    //关闭选择今天按钮
});
$('#e_time').datetimepicker({
    format:"Y-m-d",      //格式化日期
    timepicker:false,    //关闭时间选项
});
var sns = new Array();
<?php foreach ($orders as $key => $value) { ?>
    sns[<?=$key?>] = '<?=$value['sn']?>';
<?php } ?>

var data = new Array();

<?php foreach ($company as $key => $value) { ?>
    data[<?=$key?>] = new Array();
    data[<?=$key?>]['label'] = '<?=$value['name']?>';
    data[<?=$key?>]['id'] = '<?=$value['id']?>';
<?php } ?>

$(function () {
//    $("input[name='name']").autocompleter({ 
//        source: data,
//        highlightMatches: true,
//        template: '{{ label }}',
//        hint: true,
//        empty: false,
//        callback: function (value, index, selected) {
//            if (selected) {
////                $("input[name='companysn']").val(selected.id);
//            }
//        }
//    });
});

function checkform(){
    var flag = true;
    if($.trim($("input[name='sn']").val()) == ''){
        $("#snmsg").html("SN name can not be empty!");
        flag = false;
    }else{
        $("#snmsg").html("*");
    }
    
//    var companyname = $.trim($("input[name='name']").val());
//    if(companyname == ''){
//        $("#companymsg").html("The company name can not be empty!");
//        flag = false;
//    }else{
//        $("#companymsg").html("*");
//    }
    
    if($.trim($("input[name='position']").val()) == ''){
        $("#positionmsg").html("Position can not be empty!");
        flag = false;
    }else{
        $("#positionmsg").html("*");
    }
    
    if($.trim($("input[name='salary']").val()) == ''){
        $("#salarymsg").html("Salary can not be empty!");
        flag = false;
    }else{
        $("#salarymsg").html("*");
    }
    
    if(!flag){
        return flag;
    }
    
//    if(!checkEndTime()){
//        $("#timemsg").html("Start time can not greater than end time!");
//        return false;
//    }else{
//        $("#timemsg").html("");
//    }
    
    var snflag = false;
    for(var i = 0;i < sns.length;i++){
        if(sns[i] == $.trim($("input[name='sn']").val())){
            snflag = true;
        }
    }
    if(snflag){
        $("#snmsg").html("SN already exists!");
        return false;
    }else{
        $("#snmsg").html("*");
    }
    
//    var companyflag = false;
//    for(var i = 0;i < data.length;i++){
//        if(data[i]['label'] == companyname){
//            $("input[name='companysn']").val(data[i]['id']);
//            companyflag = true;
//        }
//    }
//    if(!companyflag){
//        $("input[name='companysn']").val('');
//        $("#companymsg").html("This company don't exist!");
//        return false;
//    }
    
    return flag;
}

var iTop = (document.body.clientHeight-30-600)/2; //获得窗口的垂直位置;
var iLeft = (document.body.clientWidth-10-800)/2; //获得窗口的水平位置;
$("input[name='workers']").click(function(){
    window.open('<?=base_url("profile/choice_workers")?>','_blank',
    'width=800,height=600,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,top='+iTop+',left='+iLeft); 
});

$("input[name='name']").click(function(){
    window.open('<?=base_url("profile/choice_company")?>','_blank',
    'width=800,height=600,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,top='+iTop+',left='+iLeft); 
});
</script>
　